#include "mandate.h"

//mysql 连接的数据库，dstdir：要cd的目录，id：当前用户所在目录的id，ret_info:返回信息

int cd(MYSQL* mysql,char* dstdir,int* id,char* ret_info)
{
    printf("*%s",dstdir);
    char sql[128] = {0};
    MYSQL_ROW new_id;
    sprintf(sql,"select path from file_tbl where id = %d;",*id);
    int qret = mysql_query(mysql,sql);



    if(qret != 0)
    {
        fprintf(stderr,"first SQL statement error:%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES* res = mysql_store_result(mysql);
    MYSQL_ROW curpath = mysql_fetch_row(res);
    if(dstdir[0] == '\0')
    {
        printf("before update id = %d\n",*id);
        char* new_path = strtok((char*)curpath[0],"/");
        strcat(new_path,"/");
        sprintf(sql,"select id from file_tbl where path = '%s';",new_path);
        qret = mysql_query(mysql,sql);


        if(qret != 0)
        {
            fprintf(stderr,"second SQL statement error:%s\n",mysql_error(mysql));
            return -1;
        }
        res = mysql_store_result(mysql);
        new_id = mysql_fetch_row(res);
        *id = atoi((char*)new_id[0]);
        printf("after update id = %d\n",*id);
        ls(mysql,"\0",id,ret_info);
        return 0;
    }
    char* path = strtok(dstdir,"/");
    MYSQL_ROW child_dir;
    while(path != NULL)
    {
        printf("before update id = %d\n",*id);
        if(strcmp(path,".") == 0)
        {
            continue;
        }
        if(strcmp(path,"..") == 0)
        {
            memset(sql,0,sizeof(sql));
            sprintf(sql,"select pre_id from file_tbl where id = %d",*id);
            qret = mysql_query(mysql,sql);
            if(qret != 0)
            {
                fprintf(stderr,"forth SQL statement error:%s\n",mysql_error(mysql));
                return -1;
            }
            res = mysql_store_result(mysql);
            new_id = mysql_fetch_row(res);
            if(atoi(new_id[0]) == -1)
            {
                strcpy(ret_info,"already in home  directory!\n");
                return -1;
            }
            *id = atoi((char*)new_id[0]);
            goto lable;
        }
        memset(sql,0,sizeof(sql));
        sprintf(sql,"select filename from file_tbl where pre_id = %d and type = 'dir' and is_delete = 0;",*id);
        qret = mysql_query(mysql,sql);
        if(qret != 0)
        {
            fprintf(stderr,"third SQL statement error:%s\n",mysql_error(mysql));
            return -1;
        }
        res = mysql_store_result(mysql);
        while((child_dir = mysql_fetch_row(res))!= NULL)
        {
            int i = 0;
            printf("%s\n",child_dir[0]);
            if(strcmp(path,(char*)child_dir[0]) == 0)
            {
                memset(sql,0,sizeof(sql));
                sprintf(sql,"select id from file_tbl where filename = '%s' and pre_id = %d",(char*)child_dir[0],*id);
                qret = mysql_query(mysql,sql);
                if(qret != 0)
                {
                    fprintf(stderr,"fifth SQL statement error:%s\n",mysql_error(mysql));
                    return -1;
                }
                res = mysql_store_result(mysql);
                new_id = mysql_fetch_row(res);
                *id = atoi((char*)new_id[0]);
                break;
            }
            ++i;
        }
        if(child_dir == NULL)
        {
            sprintf(ret_info,"No such directory!\n");
            return -1;
        }
lable:
        path = strtok(NULL,"/");
        printf("after update id = %d\n",*id);
    }
    ls(mysql,"\0",id,ret_info);

    return 0;
}

